package com.springboot;

import com.springboot.service.ArticleService;
import com.springboot.service.UserService;
import demo.common.util.PrintUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author yexi
 */
@SpringBootApplication
@EnableCaching
public class CacheApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(CacheApplication.class, args);

        UserService userService = context.getBean("userService", UserService.class);
        System.out.println("成功获取用户数据: " + userService.getUser());
        System.out.println("成功获取用户数据: " + userService.getUser());
        PrintUtils.println();
        System.out.println("成功获取用户数据: " + userService.getUserById(1L, "p1"));
        System.out.println("成功获取用户数据: " + userService.getUserById(1L, "p2"));
        System.out.println("成功获取用户数据: " + userService.getUserById(2L, "p3"));
        PrintUtils.println();
        System.out.println("成功获取用户数据: " + userService.getUserByParams("p1", "p1"));
        System.out.println("成功获取用户数据: " + userService.getUserByParams("p2", "p2"));
        System.out.println("成功获取用户数据: " + userService.getUserByParams("p2", "p2"));
        System.out.println("成功获取用户数据: " + userService.getUserByParams("p3", "p2"));

        ArticleService articleService = context.getBean("articleService", ArticleService.class);
        System.out.println("成功获取文章数据: " + articleService.getArticle());
        System.out.println("成功获取文章数据: " + articleService.getArticleByUserId(2L));
        PrintUtils.println();

        userService.updateDataById(1L);
        System.out.println("成功获取用户数据: " + userService.getUserById(1L, "p1"));
        PrintUtils.println();

        userService.deleteDataById(1L);
        System.out.println("成功获取用户数据: " + userService.getUserById(1L, "p1"));

        PrintUtils.println("底层存储的缓存详情");
        CacheManager cacheManager = context.getBean(CacheManager.class);
        for (String cacheName : cacheManager.getCacheNames()) {
            Cache cache = cacheManager.getCache(cacheName);
            System.out.println(cache.getNativeCache());
        }


    }

}
